"use strict";
const jwt=require("jsonwebtoken")
const { Controller } = require("egg");
const path = require('path')

class HomeController extends Controller {
  // 登录
  async login() {
    const { ctx } = this;
    const { username, password } = ctx.request.body;
    const post = await this.app.mysql.get("user", {
      user: username,
      pass: password,
    });
    if (post) {
      ctx.body = {
        code: 200,
        token:{...post}
      };
    } else {
      ctx.body = {
        code: 300,
      };
    }
  }
// 上传图片
  async upload(){
    const {ctx} = this;
    const file = ctx.request.files[0]; //取到要上传的内容
    //给上传的文件拼接目录同时起别名
    const name = 'zyy/' + Math.random().toString().substr(2,10) + path.extname(file.filename);
    try{
      let result = ctx.oss.put(name,file.filepath,{// 一定要加上请求头
        headers: {
            'Content-Type': 'image/jpg'
        }})
    }catch(err){
      ctx.body = {
        code:501,
        msg:"上传失败"
      }
      return;
    }

    ctx.body = {
      code:200,
      msg:"上传成功",
      url:`http://zyy123456.oss-cn-hangzhou.aliyuncs.com/${name}`
    }
  }

  // 获取房源数据
  async getHouses() {
    const { ctx } = this;
    const { type, cities, countryCity, secondCity, value } = ctx.query;
    const list = await this.app.mysql.select("houses");
    ctx.body = {
      code: 200,
      list: list
        .filter((v) => v.type === type)
        .filter((item) => {
          if (
            cities == "省份" ||
            secondCity == "城市" ||
            countryCity == "县区"
          ) {
            return true;
          }
          return item.address.includes(cities);
        })
        .filter((item) => {
          if (
            cities == "省份" ||
            secondCity == "城市" ||
            countryCity == "县区"
          ) {
            return true;
          }
          return item.address.includes(secondCity);
        })
        .filter((item) => {
          if (
            cities == "省份" ||
            secondCity == "城市" ||
            countryCity == "县区"
          ) {
            return true;
          }
          return item.address.includes(countryCity);
        })
        .filter((v) => {
          if (value == "") return true;
          return v.houses.includes(value);
        }),
    };
  }

// 新增房源
async addHouses() {
  const { ctx } = this;
  const { houses,pic,state,address,url} = ctx.request.body;
  const res = await this.app.mysql.insert("houses", {
    houseImg:url,
    houses,pic,type:state,address,date:new Date().toLocaleString(),num:Math.floor(Math.random()*(100-1)+1)
  });
  if (res.affectedRows === 1) {
    ctx.body = {
      code: 200,
    };
  } else {
    ctx.body = {
      code: 300,
    };
  }
}
// 编辑房源
async editHouses() {
  const { ctx } = this;
  const { houses,pic,address,key,state} = ctx.request.body;
  
  const res = await this.app.mysql.update(
    "houses",
    {houses,pic,address,type:state,date:new Date().toLocaleString() },
    { where: { key } }
  );
  if (res.affectedRows === 1) {
    ctx.body = {
      code: 200,
    };
  } else {
    ctx.body = {
      code: 300,
    };
  }
}
  // (删除公用)
  async delList() {
    const { ctx } = this;
    const { key,type} = ctx.query;
    const res = await this.app.mysql.delete(type, { key });
    if (res.affectedRows === 1) {
      ctx.body = {
        code: 200,
      };
    } else {
      ctx.body = {
        code: 300,
      };
    }
  }

  // 指派经济人
  async chooseHouses(){
    const { ctx } = this;
    const {name,key}=ctx.request.body;
    const res = await this.app.mysql.update("houses", { name },{where:{key}});
    if (res.affectedRows === 1) {
      ctx.body = {
        code: 200,
      };
    } else {
      ctx.body = {
        code: 300,
      };
    }
  }

  
   // 提交房源
   async submitHouses(){
    const { ctx } = this;
    const {key}=ctx.request.body;
    const res = await this.app.mysql.update("houses", { type1:'房源已提交' },{where:{key}});
    if (res.affectedRows === 1) {
      ctx.body = {
        code: 200,
      };
    } else {
      ctx.body = {
        code: 300,
      };
    }
  }
}

module.exports = HomeController;
